package com.mcarthur.countdown;

import javax.microedition.khronos.opengles.GL10;

import android.util.Log;

public class CountdownClock extends AbstractDrawable {
	
	public static final int WIDTH = 930;
	public static final int HEIGHT = 128;
	
	private static final int SEP = 23;
	private static final long GOOGLE_IO_TIME = 1336654800178L;
	
	private DigitGroup days;
	private DigitGroup hours;
	private DigitGroup minutes;
	private DigitGroup seconds;
	private Separator sep1;
	private Separator sep2;
	private Separator sep3;
	
	private BouncyBallManager bouncyBallManager;

	public CountdownClock(float posX) {
		super(posX);
		
		days = new DigitGroup(posX, 3, new Color(38.f/255.f, 88.f/255.f, 151/255.f));
		sep1 = new Separator(posX + days.getTotalWidth() + SEP);
		hours = new DigitGroup(sep1.getPosX() + SEP + Ball.WIDTH, 2, new Color(19.f/255.f, 172.f/255.f, 250.f/255.f));
		sep2 = new Separator(hours.getPosX() + hours.getTotalWidth() + SEP);
		minutes = new DigitGroup(sep2.getPosX() + SEP + Ball.WIDTH, 2, new Color(192.f/255.f, 0.f, 11.f/255.f));
		sep3 = new Separator(minutes.getPosX() + minutes.getTotalWidth() + SEP);
		seconds = new DigitGroup(sep3.getPosX() + SEP + Ball.WIDTH, 2, new Color(0.f, 154.f/255.f, 73.f/255.f));
		
		bouncyBallManager = BouncyBallManager.get(); 
	}

    public void draw(GL10 gl) {
    	long timeLeft = GOOGLE_IO_TIME - System.currentTimeMillis();
    	if (timeLeft > 0 && ((timeLeft % 1000) < 200)){
    		long seconds = timeLeft / 1000;
    	    long minutes = seconds / 60;
    	    long hours = minutes / 60;
    	    int days = (int)(hours / 24);
    	    if (days > 999) {
    	    	days = 999;
    	    }
    	    
    	    seconds = seconds % 60;
    	    minutes = minutes % 60;
    	    hours = hours % 24;
    	    
    	    this.seconds.setNumber((int)seconds);
    	    this.minutes.setNumber((int)minutes);
    	    this.hours.setNumber((int)hours);
    	    this.days.setNumber(days);
    	}
    	
    	days.draw(gl);
    	sep1.draw(gl);
    	hours.draw(gl);
    	sep2.draw(gl);
    	minutes.draw(gl);
    	sep3.draw(gl);
    	seconds.draw(gl);
    	
    	bouncyBallManager.draw(gl);
    }
    
    @Override
    public void setPosY(float posY) {
    	super.setPosY(posY);
    	days.setPosY(posY);
    	hours.setPosY(posY);
    	minutes.setPosY(posY);
    	seconds.setPosY(posY);
    	
    	sep1.setPosY(posY);
    	sep2.setPosY(posY);
    	sep3.setPosY(posY);
    }
}
